package com.bawei.park2302.park

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.utils.utils.entity.ParkBeanItem
import com.bawei.lib_base.utils.utils.mvi.BaseActivity
import com.bawei.lib_base.utils.utils.mvi.UiState
import com.bawei.lib_base.utils.utils.request.RequestType
import com.bawei.park2302.R
import com.bawei.park2302.databinding.ActivityParkSpacesBinding
import com.blankj.utilcode.util.ToastUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/app/ParkSpacesActivity")
@AndroidEntryPoint
class ParkSpacesActivity : BaseActivity<ActivityParkSpacesBinding,ParkIntent,ParkViewModel>() {
    override val binding: ActivityParkSpacesBinding by lazy {
        ActivityParkSpacesBinding.inflate(layoutInflater)
    }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()

    @Autowired
    lateinit var pid : String
    var area="A区停车场"
    var type="标准车位"
    var areaId=1
    var status="空闲"
    override fun initView() {
        ARouter.getInstance().inject(this)

        vm.sendIntent(ParkIntent.parkDetail(pid.toInt()))

        binding.ivSubmit.setOnClickListener {
            val carNo = binding.parkaddCarNo.text.toString()
            val remark = binding.parkaddRemark.text.toString()
            val user = binding.parkaddUser.text.toString()
            val carNum = binding.parkaddCarNum.text.toString()

            val item = ParkBeanItem(areaId,area,0,carNum,remark,carNo,status,type,0,user)

            vm.sendIntent(ParkIntent.parkSpaces(pid.toInt(),item))
        }

        binding.parkaddRg.setOnCheckedChangeListener { group, checkedId ->
            when(checkedId){
                R.id.rb_kx->status="空闲"
                R.id.rb_zy->status="已占用"
                else->status="维修中"
            }
        }
    }

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.DETAIL->{
                                val bean = it.data as ParkBeanItem
                                binding.parkaddCarNo.setText(bean.spaceNumber)
                                when(bean.spaceStatus){
                                    "空闲"->binding.parkaddRg.check(R.id.rb_kx)
                                    "已占用"->binding.parkaddRg.check(R.id.rb_zy)
                                    "维修中"->binding.parkaddRg.check(R.id.rb_wh)
                                }
                                binding.parkaddUser.setText(bean.userName)
                                binding.parkaddCarNum.setText(bean.licensePlate)
                                binding.parkaddRemark.setText(bean.remarks)

                                val areas = mutableListOf<String>("A区停车场", "B区停车场", "C区停车场")
                                if(!areas.contains(bean.areaName)){
                                    areas.add(0,bean.areaName)
                                }
                                val areaAdapter = ArrayAdapter(this@ParkSpacesActivity, android.R.layout.simple_spinner_item, areas)
                                areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                binding.parkaddArea.adapter=areaAdapter
                                binding.parkaddArea.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
                                    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long){
                                        area=areas[position]
                                        areaId=position+1
                                    }

                                    override fun onNothingSelected(parent: AdapterView<*>?) {
                                    }

                                }

                                val types = mutableListOf<String>("标准车位", "VIP车位")
                                if(!types.contains(bean.spaceType)){
                                    types.add(0,bean.spaceType)
                                }
                                val typeAdapter = ArrayAdapter(this@ParkSpacesActivity, android.R.layout.simple_spinner_item, types)
                                typeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
                                binding.parkaddType.adapter=typeAdapter
                                binding.parkaddType.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
                                    override fun onItemSelected(parent: AdapterView<*>?,view: View?,position:Int,id: Long) {
                                        type=types[position]
                                    }

                                    override fun onNothingSelected(parent: AdapterView<*>?) {
                                    }
                                }
                            }

                            RequestType.SPACE->{
                                ToastUtils.showLong("编辑成功")
                                finish()
                            }
                            else -> {}
                        }
                    }
                    else -> {}
                }
            }
        }
    }
}